﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(HairList))]
public class HairListEditor : E_ShowButtons<HairList>
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        int i = 1;
        foreach (var name in o.names)
        {
            var n = (i == 10) ? "0" : i.ToString();
            if (GUILayout.Button("（" + n + "） " + name))
            {
                o.LoadData(name);
            }
            i++;
        }
    }
}
#endif
public class HairList : MonoBehaviour
{
    [HideInInspector]
    public List<string> names;
    string saveHairNameNew
    {
        get
        {
            int i = 1;
            string post;
            while (true)
            {
                post = i == 1 ? "" : ("_" + i.ToString());
                if (!File.Exists(HairGen.I.prefabPath + saveHairName + post + postfix)) break;
                i++;
            }
            return saveHairName + post;
        }
    }
    string postfix = ".xml";
    public string saveHairName = "Prefab";
    public string loadHairNameLast;
    private void Start()
    {
        GetClipNamesInPath();
    }
    private void Update()
    {
        var n = Events.KeyAplhaDown();
        if (n > -1)
            LoadData(names[n]);
    }
    void SaveTo(string name)
    {
        if (HairGen.I.hairCurr == null || names.Count >= 10) return;
        var path = HairGen.I.prefabPath + name + postfix;
        Serializer.XMLSerialize(new HairData(HairGen.I.hairCurr), path);
        GetClipNamesInPath();
    }
    public List<string> hsn;
    public void LoadData(string name)
    {
        if (HairGen.I.hairCurr == null) return;
        loadHairNameLast = name;
        var hd = Serializer.XMLDeSerialize<HairData>(HairGen.I.prefabPath + name + postfix);
        var curr = HairGen.I.hairCurr;
        var hs = curr.curveData.relaHairs;
        hsn = new List<string>();
        Vector3 euler = Vector3.zero, scale = Vector3.zero;
        if (hs.NotEmpty())
        {
            euler = curr.curveData.pivotEuler;
            scale = curr.curveData.circleScale;
            hsn = hs.GetNames();
            foreach (var h in hs)
            {
                HairGen.I.LoadHairTo(hd.Clone(), h);
            }
        }
        curr = HairGen.I.LoadHairTo(hd, curr);
        if (hs.NotEmpty())
        {
            curr.curveData.pivotEuler = euler;
            curr.curveData.circleScale = scale;
            curr.curveData.relaHairs = HairGen.I.Find().GetComs<Hair>(hsn);
        }
    }
    void DeleteFile(string name)
    {
        var path = HairGen.I.prefabPath + name + postfix;
        File.Delete(path);
        GetClipNamesInPath();
    }
    [ShowButtonRow("保存（到新文件）", "刷新", "清空")]
    void ButtonRow(int i)
    {
        if (i == 0)
        {
            var name = saveHairNameNew;
            SaveTo(name);
            loadHairNameLast = name;
        }
        else if (i == 1) GetClipNamesInPath();
        else if (i == 2)
        {
            Directory.Delete(HairGen.I.prefabPath, true);
            GetClipNamesInPath();
        }
    }
    [ShowButtonRow("保存（覆盖最后读写的文件）", "删除")]
    void ButtonRow2(int i)
    {
        if (i == 0) SaveTo(loadHairNameLast);
        else if (i == 1) DeleteFile(loadHairNameLast);
    }
    public void GetClipNamesInPath()
    {
        names = new List<string>();

        var dir = new DirectoryInfo(HairGen.I.prefabPath);
        var fis = dir.GetFiles("*.xml", SearchOption.TopDirectoryOnly);
        foreach (var fi in fis)
        {
            var clipName = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length);
            names.Add(clipName);
        }
        names.Sort(StringTool.SortList);
    }
}
